import { syncSetTimeout, sendMessage, getMessage, dispatch } from "/js/global/tools.js";
import { nextContect, openCuiji } from "/js/anmi/modules/localTools.js";

export default class Auto {
    static count = 0;
    constructor(webController) {
        Auto.count++;
        if (Auto.count >= 2) {
            alert("collectionPlugin: \nError: Auto.count >= 2，即将刷新页面。");
            location.reload(true);
        }
        this.webController = webController;
        this.config = {
            autoNextContect: true,
            autoCall: false
        };
        // 监听事件
        {
            this.eventListenersFn = {
                click: {
                    title: "click",
                    fn: async e => {
                        if (e.target.innerText == "提交催记") {
                            // sendMessage({
                            //     from: "anmi_auto",
                            //     to: "aiccyun_call",
                            //     toUrl: "https://*.aigcyun.cn/",
                            //     title: "hangUp",
                            //     data: ""
                            // });
                            this.webController?.aiccyunHandler?.hangUp ? this.webController?.aiccyunHandler?.hangUp() : alert("【collectionPlugin】Error: anmi -> Auto.js -> hangUp() -> 调用aiccyunHandler.hangUp()失败，请检查aiccyunHandler是否正常。");
                            for (let i = 0; i < 8; i++) {
                                await syncSetTimeout(100);
                                if (jQuery("span:contains('操作成功！')").length > 0) {
                                    if (this.config.autoNextContect) {
                                        await nextContect();
                                        if (this.config.autoCall) {
                                            await this.autoCall();
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                },
                keydown: {
                    title: "keydown",
                    fn: async e => {
                        if (e.key == "=" && e.altKey) {
                            this.config.autoCall = (!this.config.autoCall);
                            alert(`【collectionPlugin】自动拨打状态: ${this.config.autoCall}`)
                        }
                        if (e.key == "/" && e.altKey) {
                            this.webController?.aiccyunHandler?.hangUp();
                            await syncSetTimeout(2000);
                            this.makeCall();
                        }
                    }
                }
            };
            // 开始监听相关事件
            for (const key in this.eventListenersFn) {
                this.webController.document().addEventListener(this.eventListenersFn[key].title, this.eventListenersFn[key].fn, this.eventListenersFn[key].capture ? true : false);
            }
        }
        this.init();
    }
    init() {
    }
    async makeCall() {
        let phoneNumber = "";
        let phoneNumberTarget = jQuery("div.refund-box > div.refund-title > span:contains('填写催记')").parent().parent().find(".mobile");
        if (phoneNumberTarget.length === 1) {
            phoneNumber = phoneNumberTarget.text();
        }
        this.webController?.aiccyunHandler?.makeCall ? this.webController?.aiccyunHandler?.makeCall(phoneNumber) : alert("【collectionPlugin】Error: anmi -> Auto.js -> makeCall() -> 调用aiccyunHandler.makeCall()失败，请检查aiccyunHandler是否正常。");
        if(this.config.autoCall) {
            for (let i = 0; i < 10; i++) {
                await syncSetTimeout(6000);
                if (!this.config.autoCall) {
                    break;
                }
                let status = this.webController?.aiccyunHandler?.config.phoneState.now;
                if ((status != "通话中") && (status != "振铃中")) {
                    this.webController?.aiccyunHandler?.makeCall ? this.webController?.aiccyunHandler?.makeCall(phoneNumber) : alert("【collectionPlugin】Error: anmi -> Auto.js -> makeCall() -> 调用aiccyunHandler.makeCall()失败，请检查aiccyunHandler是否正常。");
                } else {
                    break;
                }
            }
        }
    }
    async autoCall() {
        for (let i = 0; i < 11; i++) {
            await syncSetTimeout(700);
            if (jQuery(".ivu-modal-content div.ivu-modal-confirm-head-title:contains('操作失败')").length >= 1) {
                break;
            }
            if (jQuery(".contact-info .right i.icon-cuiji").length != 0 && jQuery(".ivu-modal-content div.ivu-modal-confirm-head-title:contains('操作失败')").length === 0 && jQuery(".ivu-spin-dot").length === 0) {
                // 如果合同标记了红颜色，就提示是否跳过这个合同
                if (jQuery("div.agent-info div.case-container div.sign-color")[0]?.style?.backgroundColor == 'rgb(244, 63, 63)') {
                    let jump = prompt("[collectionPlugin]是否继续拨打此合同？(输入1继续拨打,输入其他字符跳过此合同)");
                    if (jump == "1") {
                        await openCuiji(true);
                        await syncSetTimeout(100);
                        this.makeCall();
                        break;
                    } else {
                        if (this.config.autoNextContect) {
                            await nextContect();
                            if (this.config.autoCall) {
                                await this.autoCall();
                            }
                        }
                        break;
                    }
                } else {
                    await openCuiji(true);
                    await syncSetTimeout(100);
                    this.makeCall();
                    break;
                }
            }
        }
    }
}